import java.util.ArrayList;
import java.util.List;

//链表变成数组打印
public class LinkedListToArray {
    public static int[] linkedListToArray(ListNode head) {
        //使用ArrayList来存储链表节点的值 然后将ArrayList转换成数组
        List<Integer> list = new ArrayList<>();
        ListNode current = head;
        while (current != null) {
            list.add(current.val);
            current = current.next;
        }

        int[] array = new int[list.size()];
        for (int i = 0; i < list.size(); i++) {
            array[i] = list.get(i);
        }

        return array;
    }
}
